/*
 * @(#)Ecs.java   1.0  2021年9月13日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.api.aliyun.client;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.swift.exception.extend.SystemException;
import com.swift.util.type.TypeUtil;

/**
 * 添加说明 
 * @author zhengjiajin
 * @version 1.0 2021年9月13日
 */
@Component
public class EcsClient {
    
    @Value("${aliyun.AccessKey:}")
    private String accessKey;
    
    @Value("${aliyun.SecretKey:}")
    private String secretKey;
    
    @Value("${aliyun.RegionId:}")
    private String regionId;

    private IAcsClient clientesc;
    
    @PostConstruct
    private void init() {
        if(TypeUtil.isNull(accessKey)) return;
        if(TypeUtil.isNull(secretKey)) return;
        if(TypeUtil.isNull(regionId)) return;
        
        IClientProfile profile = DefaultProfile.getProfile(regionId, accessKey, secretKey);
        clientesc = new DefaultAcsClient(profile);  
    }
    
    public IAcsClient getClient() {
        if(clientesc==null) new SystemException("阿里客户端未创建");
        return clientesc;
    }
    

    @PreDestroy
    private void destroy() {
        if(clientesc!=null) clientesc.shutdown();
    }
}
